package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author tongatong
 * @name CartController
 * @description
 * @date 2021/9/24
 * @time 12:13
 */
@Controller
public class CartController {
    @Resource
    private CartFeignClient cartFeignClient;
    @Resource
    private ProductFeignClient productFeignClient;


    /**
     * 查看购物车
     * @param request http请求
     * @return 返回页面路径
     */
    @GetMapping("cart.html")
    public String index(HttpServletRequest request){
        return "cart/index";
    }

    /**
     * 添加购物车
     * @param skuId skuId
     * @param skuNum 购买数量
     * @param request 请求
     * @return 返回页面路径
     */
    @GetMapping("addCart.html")
    public String addCart(@RequestParam(name = "skuId") Long skuId,
                          @RequestParam(name = "skuNum") Integer skuNum,
                          HttpServletRequest request){
        //前端已经发送异步请求了，所以这里并不需要再次调用添加购物车方法了
//        cartFeignClient.addToCart(skuId, skuNum);

        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return "cart/addCart";
    }

}
